package net.jsoft.platform.activiti.web;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import net.jsoft.platform.core.web.BaseController;

/**
 * @author bootdo 1992lcg@163.com
 */
@RequestMapping("/activiti")
@Controller
public class ModelController extends BaseController
{

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ObjectMapper objectMapper;

    private String PREFIX = "/platform/activiti/";

    @RequestMapping("/list")
    public String model()
    {
        return PREFIX + "model";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public JSONObject listData(int pageSize, int pageNumber)
    {
        int offset = (pageNumber - 1) * pageSize;
        List<Model> list = repositoryService.createModelQuery().listPage(offset, pageSize);
        int total = (int) repositoryService.createModelQuery().count();
        JSONObject json = new JSONObject();
        json.put("rows", list); //这里的 rows 和total 的key 是固定的 
        json.put("total", total);
        return json;
    }

    @RequestMapping("/add")
    public void newModel(HttpServletResponse response) throws UnsupportedEncodingException
    {

        //初始化一个空模型
        Model model = repositoryService.newModel();
        //设置一些默认信息
        String name = "new-process";
        String description = "";
        int revision = 1;
        String key = "process";

        ObjectNode modelNode = objectMapper.createObjectNode();
        modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);
        modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
        modelNode.put(ModelDataJsonConstants.MODEL_REVISION, revision);

        model.setName(name);
        model.setKey(key);
        model.setMetaInfo(modelNode.toString());

        repositoryService.saveModel(model);
        String id = model.getId();

        //完善ModelEditorSource
        ObjectNode editorNode = objectMapper.createObjectNode();
        editorNode.put("id", "canvas");
        editorNode.put("resourceId", "canvas");
        ObjectNode stencilSetNode = objectMapper.createObjectNode();
        stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
        editorNode.set("stencilset", stencilSetNode);
        repositoryService.addModelEditorSource(id, editorNode.toString().getBytes("utf-8"));
        try
        {
            response.sendRedirect("/activiti/modeler.html?modelId=" + id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    //
    //    @GetMapping(value = "/model/{modelId}/json")
    //    public ObjectNode getEditorJson(@PathVariable String modelId)
    //    {
    //        ObjectNode modelNode = null;
    //        Model model = repositoryService.getModel(modelId);
    //        if (model != null)
    //        {
    //            try
    //            {
    //                if (StringUtils.isNotEmpty(model.getMetaInfo()))
    //                {
    //                    modelNode = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
    //                }
    //                else
    //                {
    //                    modelNode = objectMapper.createObjectNode();
    //                    modelNode.put(MODEL_NAME, model.getName());
    //                }
    //                modelNode.put(MODEL_ID, model.getId());
    //                ObjectNode editorJsonNode = (ObjectNode) objectMapper.readTree(new String(repositoryService.getModelEditorSource(model.getId()), "utf-8"));
    //                modelNode.put("model", editorJsonNode);
    //
    //            }
    //            catch (Exception e)
    //            {
    //                LOGGER.error("Error creating model JSON", e);
    //                throw new ActivitiException("Error creating model JSON", e);
    //            }
    //        }
    //        return modelNode;
    //    }
    //

    @RequestMapping("/edit/{id}")
    public void edit(HttpServletResponse response, @PathVariable("id") String id)
    {
        try
        {
            response.sendRedirect("/activiti/modeler.html?modelId=" + id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String remove(@PathVariable("id") String id)
    {
        repositoryService.deleteModel(id);
        return "ok";
    }

    @RequestMapping("/deploy/{id}")
    @ResponseBody
    public String deploy(@PathVariable("id") String id) throws Exception
    {
        //获取模型
        Model modelData = repositoryService.getModel(id);
        byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());

        if (bytes == null)
        {
            return "模型数据为空，请先设计流程并成功保存，再进行发布。";
        }

        JsonNode modelNode = new ObjectMapper().readTree(bytes);

        BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
        if (model.getProcesses().size() == 0)
        {
            return "数据模型不符要求，请至少设计一条主线流程。";
        }
        byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);

        //发布流程
        String processName = modelData.getName() + ".bpmn20.xml";
        Deployment deployment = repositoryService.createDeployment().name(modelData.getName()).addString(processName, new String(bpmnBytes, "UTF-8")).deploy();
        modelData.setDeploymentId(deployment.getId());
        repositoryService.saveModel(modelData);

        return "ok";
    }

    @RequestMapping(value = "/model/{modelId}/save", method = RequestMethod.PUT)
    @ResponseBody
    public void saveModel(@PathVariable String modelId, String name, String description, String json_xml, String svg_xml)
    {
        try
        {

            Model model = repositoryService.getModel(modelId);

            ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());

            modelJson.put(ModelDataJsonConstants.MODEL_NAME, name);
            modelJson.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
            model.setMetaInfo(modelJson.toString());
            model.setName(name);

            repositoryService.saveModel(model);

            repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));

            InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));
            TranscoderInput input = new TranscoderInput(svgStream);

            PNGTranscoder transcoder = new PNGTranscoder();
            // Setup output
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            TranscoderOutput output = new TranscoderOutput(outStream);

            // Do the transformation
            transcoder.transcode(input, output);
            final byte[] result = outStream.toByteArray();
            repositoryService.addModelEditorSourceExtra(model.getId(), result);
            outStream.close();

        }
        catch (Exception e)
        {
            throw new ActivitiException("Error saving model", e);
        }
    }

    @RequestMapping("/export/{id}")
    public void exportToXml(@PathVariable("id") String id, HttpServletResponse response)
    {
        try
        {
            org.activiti.engine.repository.Model modelData = repositoryService.getModel(id);
            BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
            JsonNode editorNode = new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
            BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);
            BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
            byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);

            ByteArrayInputStream in = new ByteArrayInputStream(bpmnBytes);
            IOUtils.copy(in, response.getOutputStream());
            String filename = bpmnModel.getMainProcess().getId() + ".bpmn20.xml";
            response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            response.flushBuffer();
        }
        catch (Exception e)
        {
            throw new ActivitiException("导出model的xml文件失败，模型ID=" + id, e);
        }
    }
}
